<?php


namespace app\middleware;


use app\models\AdminModel;
use app\Request;

class Auth
{

    public function handle(Request $request, \Closure $next)
    {

        $token = $request->header('authorization') ?? $request->get('authorization');
        $token = str_replace("Bearer ", "", $token);


        if (empty($token)) return retErrParam("登录已失效","402");


        if (false === $payload = app()->make('jwt')->verifyToken($token))
        {
            return retErrParam("登录已失效","402");
        }
        $userModel = (new AdminModel())->where([
            'id'=>$payload['uid'],
        ])->findOrEmpty();



        if ($userModel->isEmpty()) {
            return retErrParam("登录已失效","402");
        }

        app('auth')->setUser($userModel);

        return $next($request);

    }

}
